package com.zjsd.sqeybusiness.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.zjsd.sqeybusiness.domain.Dlaccount;
import com.zjsd.sqeybusiness.mapper.DlaccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zjsd.sqeybusiness.mapper.DlpaymentMapper;
import com.zjsd.sqeybusiness.domain.Dlpayment;
import com.zjsd.sqeybusiness.service.IDlpaymentService;

/**
 * 代理账户变动明细Service业务层处理
 *
 * @author Zheng
 * @date 2022-05-10
 */
@Service
public class DlpaymentServiceImpl implements IDlpaymentService
{
    @Autowired
    private DlpaymentMapper dlpaymentMapper;
    @Autowired
    private DlaccountMapper dlaccountMapper ;

    @Autowired
    private SysDeptMapper sysDeptMapper ;
    /**
     * 查询代理账户变动明细
     *
     * @param paymentid 代理账户变动明细ID
     * @return 代理账户变动明细
     */
    @Override
    public Dlpayment selectDlpaymentById(Long paymentid)
    {
        return dlpaymentMapper.selectDlpaymentById(paymentid);
    }

    /**
     * 查询代理账户变动明细列表
     *
     * @param dlpayment 代理账户变动明细
     * @return 代理账户变动明细
     */
    @Override
    public List<Dlpayment> selectDlpaymentList(Dlpayment dlpayment)
    {
        return dlpaymentMapper.selectDlpaymentList(dlpayment);
    }

    /**
     * 新增代理账户变动明细
     *
     * @param dlpayment 代理账户变动明细
     * @return 结果
     */
    @Override
    public int insertDlpayment(Dlpayment dlpayment)
    {

        Long deptid = dlpayment.getDeptid() ;
        SysDept sysDept = sysDeptMapper.selectDeptById(deptid);

        Dlaccount dlaccount = new Dlaccount();
        dlaccount.setDeptid(dlpayment.getDeptid());
        dlaccount.setTcid(dlpayment.getTcid());
        List<Dlaccount> dlaccountList = dlaccountMapper.selectDlaccountList(dlaccount);
        BigDecimal bdqje = BigDecimal.ZERO ;
        BigDecimal bdhje = BigDecimal.ZERO ;

        BigDecimal djje = dlpayment.getDjje() ; // 所选套餐代缴金额
        BigDecimal bcjfje = dlpayment.getBcjfje() ; // 本次客户缴费金额
        dlpayment.setSjje(dlpayment.getBcjfje());

        if(dlaccountList.size() > 0 ){
            Dlaccount dlaccount1 = dlaccountList.get(0);
            BigDecimal zhye = dlaccount1.getZhye() != null ? dlaccount1.getZhye():BigDecimal.ZERO ;
            BigDecimal sjje = dlaccount1.getSjje() != null ? dlaccount1.getSjje():BigDecimal.ZERO ;
            BigDecimal bdqkhsjje = sjje ;
            BigDecimal bdhkhsjje = BigDecimal.ZERO ;
            bdqje = zhye ;
            BigDecimal sumsjje = sjje.add(bcjfje);
            // 充值
            if("1".equals(dlpayment.getBdlx())){
                // 判断本次充值金额加客户账户实缴金额等于套餐代缴金额时候，给套餐充值账户充值，若不足，则保存至客户实缴账户
                if(sumsjje.compareTo(djje) >= 0 ){
                    bdhje = bdqje.add(dlpayment.getCzje() );
                    bdhkhsjje = sumsjje.subtract(djje);
                    sysDept.setQyzt("1");
                    sysDeptMapper.updateDept(sysDept);
                    dlpayment.setBz("实交金额已充值到套餐账户");
                    dlaccount1.setBz("");
                }else {
                    dlpayment.setCzje(BigDecimal.ZERO);
                    bdhkhsjje = bdqkhsjje.add(bcjfje);
                    BigDecimal zjfje = djje.subtract(sumsjje);
                    String bz = "已缴费" + sumsjje +  "元，再交费" + zjfje + "元即可成为签约代理" ;
                    dlaccount1.setBz(bz);
                    dlpayment.setBz(bz);
                }
                dlpayment.setBdqkhsjje(bdqkhsjje);
                dlpayment.setBdhkhsjje(bdhkhsjje);
            }

            //消费
            if("2".equals(dlpayment.getBdlx())){
                if(bdqje.compareTo(dlpayment.getCzje()) < 0){
                    throw new CustomException("余额不足");
                }
                bdhje = bdqje.subtract(dlpayment.getCzje());
            }
            dlaccount1.setZhye(bdhje);
            dlaccount1.setSjje(sumsjje);
            dlaccount1.setSjje(bdhkhsjje);
            dlaccountMapper.updateDlaccount(dlaccount1);
        }else {

            BigDecimal zhye = BigDecimal.ZERO ;
            BigDecimal sjje = BigDecimal.ZERO ;
            BigDecimal bdqkhsjje = sjje ;
            BigDecimal bdhkhsjje = BigDecimal.ZERO ;
            bdqje = zhye ;
            BigDecimal sumsjje = sjje.add(bcjfje);
            // 充值
            if("1".equals(dlpayment.getBdlx())){
                // 判断本次充值金额加客户账户实缴金额等于套餐代缴金额时候，给套餐充值账户充值，若不足，则保存至客户实缴账户
                if(sumsjje.compareTo(djje) >= 0 ){
                    bdhje = bdqje.add(dlpayment.getCzje() );
                    bdhkhsjje = sumsjje.subtract(djje);
                    sysDept.setQyzt("1");
                    sysDeptMapper.updateDept(sysDept);
                    dlpayment.setBz("实交金额已充值到套餐账户");
                    dlaccount.setBz("");
                }else {
                    dlpayment.setCzje(BigDecimal.ZERO);
                    bdhkhsjje = bdqkhsjje.add(bcjfje);
                    BigDecimal zjfje = djje.subtract(sumsjje);
                    String bz = "已缴费" + sumsjje +  "元，再交费" + zjfje + "元即可成为签约代理" ;
                    dlaccount.setBz(bz);
                }
                dlpayment.setBdqkhsjje(bdqkhsjje);
                dlpayment.setBdhkhsjje(bdhkhsjje);

            }

            // 消费
            if("2".equals(dlpayment.getBdlx())){
                if(bdqje.compareTo(dlpayment.getCzje()) < 0){
                    throw new CustomException("余额不足");
                }
                bdhje = bdqje.subtract(dlpayment.getCzje());
            }

            dlaccount.setSjje(sumsjje);
            dlaccount.setZhye(bdhje);
            dlaccount.setSjje(bdhkhsjje);
            dlaccountMapper.insertDlaccount(dlaccount);
        }
        dlpayment.setBdhje(bdhje);
        dlpayment.setBdqje(bdqje);
        dlpayment.setFssj(new Date());;
        return dlpaymentMapper.insertDlpayment(dlpayment);
    }

    /**
     * 修改代理账户变动明细
     *
     * @param dlpayment 代理账户变动明细
     * @return 结果
     */
    @Override
    public int updateDlpayment(Dlpayment dlpayment)
    {
        return dlpaymentMapper.updateDlpayment(dlpayment);
    }

    /**
     * 批量删除代理账户变动明细
     *
     * @param paymentids 需要删除的代理账户变动明细ID
     * @return 结果
     */
    @Override
    public int deleteDlpaymentByIds(Long[] paymentids)
    {
        return dlpaymentMapper.deleteDlpaymentByIds(paymentids);
    }

    /**
     * 删除代理账户变动明细信息
     *
     * @param paymentid 代理账户变动明细ID
     * @return 结果
     */
    @Override
    public int deleteDlpaymentById(Long paymentid)
    {
        return dlpaymentMapper.deleteDlpaymentById(paymentid);
    }
}
